<?php
namespace Admin\Controller;
use Admin\Common\BaseController;
class NoticeController extends BaseController {
    public function index(){
    	$notice = M('notice');
    	$search = I('get.search');

    	if(!empty($search)){
    		$where = "title like '%".$search."%'";
    	}else{
    		$where = '';
    	}

    	$count = $notice -> where($where) -> count();
    	$page = new \Think\Page($count,5);
    	$show = $page -> show();
    	$limit = $page -> firstRow.','.$page -> listRows;
    	$rr = $notice -> limit($limit) -> where($where) -> order('time desc') -> select();

    	$data = array();
    	foreach ($rr as $k => $v) {
    		$data[$k]['id'] = $v['id'];
            $data[$k]['team'] = $v['team'];
            $data[$k]['source'] = $v['source'];
            $data[$k]['title'] = $v['title'];
            $data[$k]['content'] = htmlspecialchars_decode(mb_substr($v['content'],0,50));
            $data[$k]['time'] = $v['time'];
    	}

    	$this -> pagestr = $show;
    	$this -> assign('data',$data);
    	$this -> assign('search',$search);
        $this -> display();
    }

    public function add(){
    	$this -> display();
    }

    public function insert(){
    	$notice = M('notice');
    	$notice -> create();
    	$res = $notice -> add();
    	if($res){
    		echo "<script>
                    alert('添加成功');
                    window.location.href='".U('Admin/Notice/index')."';
                </script>";
        }else{
        	echo "<script>
                    alert('添加失败');
                    window.location.href='".U('Admin/Notice/index')."';
                </script>";
        }

    }

    public function delete(){
    	$id = (int)I('post.id');
    	$notice = M('notice');
    	$res = $notice -> where('id = "'.$id.'"') -> delete();
    	if($res){
    		echo 1;
    	}else{
    		echo 2;
    	}
    }

    public function del(){
    	$id = I('post.id');
    	$notice = M('notice');
    	$did = implode(',', $id);
    	$where['id'] = array('in',$did);
    	$res = $notice -> where($where) -> delete();
    	if($res){
    		echo "<script>
                alert('删除成功');
                window.location.href='".U('Admin/Notice/index')."';
            </script>";
    	}else{
    		echo "<script>
                alert('删除失败');
                window.location.href='".U('Admin/Notice/index')."';
            </script>";
    	}
    }

    public function edit(){
        $id = (int)I('get.id');
        $notice = M('notice');
        $data = $notice -> where('id = "'.$id.'"') -> find();
        $data['content'] = htmlspecialchars_decode($data['content']);

        $this -> assign('data',$data);
        $this -> display();
    }

    public function update(){
        $notice = M('notice');
        $id = (int)I('post.id');
        $notice -> create();
        $res = $notice -> save();
        if($res){
            echo "<script>
                    alert('用户修改成功');
                    window.location.href='".U('Admin/Notice/index')."';
            </script>";
        }else{
            echo "<script>
                    alert('用户修改失败');
                    window.location.href='".U('Admin/Notice/edit',array('id'=>$id))."';
            </script>";
        }
    }
}